package com.hns.frame.security;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
/**
 * 登录用户信息类
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class AppUserDetailsImpl implements UserDetails, Serializable {
	
	private static final long serialVersionUID = 3631850476763400129L;
	private SysUserEntity user;
	private Set<SysRoleEntity> set_role;
	public AppUserDetailsImpl(SysUserEntity user, Set<SysRoleEntity> set_role) {
		this.user = user;
		this.set_role = set_role;
	}

	/**
	 * 获取权限
	 * @return
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public Collection<GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> col_auth = new ArrayList<GrantedAuthority>();
		Set<SysRoleEntity> set_role = getSet_role();
		for (SysRoleEntity r : set_role) {
			col_auth.add(new SimpleGrantedAuthority(r.getId()));
		}
		
		return col_auth;
	}
	
	public String getUsername() {
		return String.valueOf(getUser().getAcctName());
	}

	public String getPassword() {
		return getUser().getPassword();
	}
	
	public String getName() {
        return getUser().getName();
    }
	
	public String getLoginType() {
		return getUser().getLoginType();
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
	    return true;
	}

	public SysUserEntity getUser() {
		return user;
	}

	public Set<SysRoleEntity> getSet_role() {
		return set_role;
	}
	
	public boolean equals(Object obj) {
		return canEqual(obj) && ((AppUserDetailsImpl) obj).getUser().getId().equals(getUser().getId());
	}
	
	public int hashCode() {
		return getUser().getId().hashCode();
	}
	
	protected boolean canEqual(Object obj) {
		return obj instanceof AppUserDetailsImpl;
	}
}
